home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / AVIOutputImages.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  4.3 KB  |  164 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <stdio.h>
  19. #include <crtdbg.h>
  20.  
  21. #include "VideoSource.h"
  22.  
  23. #include "Error.h"
  24. #include "AVIOutput.h"
  25. #include "AVIOutputImages.h"
  26.  
  27. class AVIOutputImages;
  28.  
  29. ////////////////////////////////////
  30.  
  31. class AVIAudioImageOutputStream : public AVIAudioOutputStream {
  32. public:
  33.     AVIAudioImageOutputStream(AVIOutput *out);
  34.  
  35.     BOOL write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples);
  36.     BOOL finalize();
  37.     BOOL flush();
  38. };
  39.  
  40. AVIAudioImageOutputStream::AVIAudioImageOutputStream(AVIOutput *out) : AVIAudioOutputStream(out) {
  41. }
  42.  
  43. BOOL AVIAudioImageOutputStream::write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples) {
  44.     return TRUE;
  45. }
  46.  
  47. BOOL AVIAudioImageOutputStream::finalize() {
  48.     return TRUE;
  49. }
  50.  
  51. BOOL AVIAudioImageOutputStream::flush() {
  52.     return TRUE;
  53. }
  54.  
  55. ////////////////////////////////////
  56.  
  57. class AVIVideoImageOutputStream : public AVIVideoOutputStream {
  58. private:
  59.     DWORD dwFrame;
  60.     char *szFormat;
  61.     int iDigits;
  62.  
  63. public:
  64.     AVIVideoImageOutputStream(AVIOutput *out, char *szFormat, int iDigits);
  65.  
  66.     BOOL write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples);
  67.     BOOL finalize();
  68. };
  69.  
  70. AVIVideoImageOutputStream::AVIVideoImageOutputStream(AVIOutput *out, char *szFormat, int iDigits) : AVIVideoOutputStream(out) {
  71.     this->szFormat        = szFormat;
  72.     this->iDigits        = iDigits;
  73.  
  74.     dwFrame = 0;
  75. }
  76.  
  77. BOOL AVIVideoImageOutputStream::write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples) {
  78.     BITMAPFILEHEADER bfh;
  79.     char szFileName[MAX_PATH];
  80.     HANDLE hFile;
  81.     DWORD dwActual;
  82.  
  83.     sprintf(szFileName, szFormat, iDigits, dwFrame++);
  84.  
  85.     hFile = CreateFile(
  86.                 szFileName,
  87.                 GENERIC_WRITE,
  88.                 0,
  89.                 NULL,
  90.                 CREATE_ALWAYS,
  91.                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  92.                 NULL
  93.                 );
  94.  
  95.     if (!hFile) return FALSE;
  96.  
  97.     bfh.bfType        = 'MB';
  98.     bfh.bfSize        = sizeof(BITMAPFILEHEADER)+getFormatLen()+cbBuffer;
  99.     bfh.bfReserved1    = 0;
  100.     bfh.bfReserved2    = 0;
  101.     bfh.bfOffBits    = sizeof(BITMAPFILEHEADER)+getFormatLen();
  102.  
  103.     try {
  104.         if (!WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwActual, NULL) || dwActual != sizeof(BITMAPFILEHEADER))
  105.             throw 0;
  106.         if (!WriteFile(hFile, getFormat(), getFormatLen(), &dwActual, NULL) || dwActual != getFormatLen())
  107.             throw 0;
  108.         if (!WriteFile(hFile, lpBuffer, cbBuffer, &dwActual, NULL) || dwActual != cbBuffer)
  109.             throw 0;
  110.     } catch(int) {
  111.         CloseHandle(hFile);
  112.         throw MyWin32Error("Error writing image: %%s", GetLastError());
  113.     }
  114.  
  115.     if (!CloseHandle(hFile)) return FALSE;
  116.  
  117.     return TRUE;
  118. }
  119.  
  120. BOOL AVIVideoImageOutputStream::finalize() {
  121.     return TRUE;
  122. }
  123.  
  124. ////////////////////////////////////
  125.  
  126. AVIOutputImages::AVIOutputImages(char *szFormatString, int digits) {
  127.     strcpy(this->szFormat, szFormatString);
  128.     this->iDigits        = digits;
  129. }
  130.  
  131. AVIOutputImages::~AVIOutputImages() {
  132. }
  133.  
  134. //////////////////////////////////
  135.  
  136. BOOL AVIOutputImages::initOutputStreams() {
  137.     if (!(audioOut = new AVIAudioImageOutputStream(this))) return FALSE;
  138.     if (!(videoOut = new AVIVideoImageOutputStream(this, szFormat, iDigits))) return FALSE;
  139.  
  140.     return TRUE;
  141. }
  142.  
  143. BOOL AVIOutputImages::init(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved) {
  144.     if (audioIn) {
  145.         if (!audioOut) return FALSE;
  146.     } else {
  147.         delete audioOut;
  148.         audioOut = NULL;
  149.     }
  150.  
  151.     if (!videoOut) return FALSE;
  152.  
  153.     return TRUE;
  154. }
  155.  
  156. BOOL AVIOutputImages::finalize() {
  157.     return TRUE;
  158. }
  159.  
  160. BOOL AVIOutputImages::isPreview() { return FALSE; }
  161.  
  162. void AVIOutputImages::writeIndexedChunk(FOURCC ckid, LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer) {
  163. }
  164.